import {getToken,getRefreshToken,setToken, setRefreshToken, removeToken, removeRefreshToken} from '@/utils/cache'
import {setSessionStore,getSessionStore} from "../../utils/store";
import {getCustomer} from "../../request/api";

const user = {
  state: {
    token: getToken(),
    refreshToken: getRefreshToken(),

    customerId: getSessionStore('customerId') || '',
    customerName: getSessionStore('customerName') || '',
    customerPhone: getSessionStore('customerPhone') || '',

    isLogin: false,

    companyName: null,
    companyId: null,
    userName: null,
    userId: null,
    departmentName: null,
    departmentId: null,
    employeeRowId: null
  },
  actions: {
    login({ commit }, data){
      let {token,refresh_token,company_name,company_id,user_name,user_id,department_id,department_name,employee_row_id} = data;

      commit('SET_TOKEN', token);
      commit('SET_REFRESH_TOKEN', refresh_token);
      commit('SET_LOGIN', true);
      commit('SET_COMPANY_NAME', company_name);
      commit('SET_COMPANY_ID',company_id);
      commit('SET_USER_NAME',user_name);
      commit('SET_USER_ID', user_id);
      commit('SET_DEPARTMENT_NAME', department_name);
      commit('SET_DEPARTMENT_ID', department_id);
      commit('SET_EMPLOYEE_ROW_ID', employee_row_id);
    },

    logout({ commit}){
      removeToken();
      removeRefreshToken();

      commit('SET_TOKEN', '');
      commit('SET_REFRESH_TOKEN', '');
      commit('SET_LOGIN', false);
      commit('SET_COMPANY_NAME', '');
      commit('SET_COMPANY_ID','');
      commit('SET_USER_NAME','');
      commit('SET_USER_ID', '');
      commit('SET_DEPARTMENT_NAME', '');
      commit('SET_DEPARTMENT_ID', '');
      commit('SET_EMPLOYEE_ROW_ID', '');
    },

    getCustomer({commit}){
      return new Promise(resolve => {
        getCustomer().then(res => {
          if (res.errcode !== 0) return;
          let data = res.row[0],
            {customer_id,customer_name,phone} = data || {};
          commit('SET_CUSTOMER_ID',customer_id)
          commit('SET_CUSTOMER_NAME',customer_name)
          commit('SET_CUSTOMER_PHONE',phone)
          // this.dispatch('setCustomer',data);
          resolve();
        })
      })
    },
    setCustomer({ commit }, data){
      let {customer_id,customer_name,phone} = data;
      commit('SET_CUSTOMER_ID',customer_id)
      commit('SET_CUSTOMER_NAME',customer_name)
      commit('SET_CUSTOMER_PHONE',phone)
    }
  },
  mutations: {
    SET_TOKEN: (state,token) => {
      setToken(token);
      state.token = token;
    },
    SET_REFRESH_TOKEN: (state, refreshToken) => {
      setRefreshToken(refreshToken);
      state.refreshToken = refreshToken;
    },
    SET_LOGIN: (state,type) =>{
      state.isLogin = type;
    },
    SET_COMPANY_NAME: (state,name)=>{
      state.companyName = name;
    },
    SET_COMPANY_ID: (state,id)=>{
      state.companyId = id;
    },
    SET_USER_NAME: (state,name)=>{
      state.userName = name;
    },
    SET_USER_ID: (state,id)=>{
      state.userName = id;
    },
    SET_DEPARTMENT_NAME: (state,name)=>{
      state.departmentName = name;
    },
    SET_DEPARTMENT_ID: (state,id)=>{
      state.departmentId = id;
    },
    SET_EMPLOYEE_ROW_ID: (state,id)=>{
      state.employeeRowId = id;
    },
    SET_CUSTOMER_ID: (state,id) =>{
      setSessionStore('customerId',id)
      state.customerId = id;
    },
    SET_CUSTOMER_NAME: (state,name) =>{
      setSessionStore('customerName',name)
      state.customerName = name;
    },
    SET_CUSTOMER_PHONE: (state,phone) =>{
      setSessionStore('customerPhone',phone)
      state.customerPhone = phone;
    }
  }
}
export default user
